$(function () {
    $("#jqGrid").jqGrid({
        url: '/sys/jsms/list',
        datatype: "json",
        colModel: [
            { label: 'smsRecordId', name: 'smsRecordId', index: 'smsRecordId', width: 50 ,key:true,hidden:true},
            { label: 'app名称', name: 'appId.appName', index: 'appId.appName', width: 80 },
            // { label: 'app关键字', name: 'appKey', index: 'appKey', width: 80 },
            { label: '发送时间', name: 'sendDate', index: 'sendDate', width: 80 },
            { label: '短信类型', name: 'smsType', index: 'smsType', width: 50, formatter: function(value, options, row){
                if (value == 0){
                    return '<span class="label label-info">普通短信</span>';
                } else if (value == 1){
                    return '<span class="label label-danger">验证码</span>';
                } else if (value == 2){
                    return '<span class="label label-success">单条模板短信</span>';
                } else if (value == 3){
                    return '<span class="label label-inverse">批量模板短信</span>';
                } else if (value == 4){
                    return '<span class="label label-warning">定时短信</span>';
                }
                return '';
                },
                unformat:function (value,op,row) {
                    if (value == '<span class="label label-info">普通短信</span>')
                        return 0;
                    else if (value == '<span class="label label-danger">验证码</span>')
                        return 1;
                    else if (value == '<span class="label label-success">模板短信</span>')
                        return 2;
                    else if (value == '<span class="label label-warning">定时短信</span>')
                        return 3;
                }
            },
            { label: '接收号码', name: 'smsMobile', index: 'smsMobile', width: 80 },
            { label: '短信内容', name: 'smsContent', index: 'smsContent', width: 80 },
            { label: '备注', name: 'remake', index: 'remake', width: 50 },
            {}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "data.list",
            page: "data.currPage",
            total: "data.totalPage",
            records: "data.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        loadComplete:function (data) {
            console.log("data",data);
        },
        gridComplete:function(){
            //隐藏grid底部滚动条\
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    $("#startDate").flatpickr({
        dateFormat:'Y-m-d',
        "locale":"zh"
    });
    $("#endDate").flatpickr({
        dateFormat:'Y-m-d',
        "locale":"zh"
    });
    vm.loadApps();

});

var vm = new Vue({
    el:"#rrapp",
    data:{
        showList:true,
        apps:[],
        sms:{},
        title:"",
        isSchedule:false,
    },
    methods:{
        loadApps:function () {
            $.ajax({
                url:"/sys/app/list?page=1&limit=1000",
                type:"POST",
                dataType:"JSON",
                success:function (result) {
                    if (result.code == 0){
                        vm.apps = result.data.list;
                    }else
                        alert(result.msg);
                },
                error:function (e) {
                    console.log("error",e);
                    alert("请求失败，请重试")
                }
            })
        },
        scheduleChange:function (e) {
            var hasChk = $('#schedule_box').is(':checked');
            vm.isSchedule = hasChk;
            if (hasChk){
                $("#sendDate_input").flatpickr({
                    dateFormat:'Y-m-d H:i:S',
                    enableTime:true,
                    time_24hr:true,
                    "locale":"zh"
                });
            }
        },
        query:function () {
            
        },
        info:function () {
            
        },
        sendSMS:function () {
            vm.showList = false;
            vm.title = "发送消息";
        },
        send:function () {
            if (vm.sms.appId == 1){
                alert("请选择app");
                return;
            }
            var mobiles = vm.sms.smsMobile.split(",");
            if (mobiles.length == 0){
                alert("请输入接收手机号码");
                return;
            }
            if (vm.isSchedule)
                vm.sms.type = 1;
            else if (mobiles.length == 1)
                vm.sms.type = 2;
            else if (mobiles.length > 1)
                vm.sms.type = 3;
            console.log("data",JSON.parse(JSON.stringify(vm.sms)));
            $.ajax({
                url:'/sys/jsms/sendSMS',
                type:'POST',
                dataType:'JSON',
                data:JSON.stringify(vm.sms),
                success:function (result) {
                    if (result.code == 0){
                        alert("操作成功");
                        vm.reload();
                    }else {
                        alert(result.msg);
                    }
                },
                error:function (e) {
                    alert("请求失败");
                    console.error(e);
                }
            });
        },
        cancel:function () {
            vm.showList = true;
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                page:page
            }).trigger("reloadGrid");
        }
    }
});